extends BaseEnemy

class_name FlyingCreature

# 飞行生物特定属性
@export var fly_height: float = 20.0
@export var hover_amplitude: float = 5.0
@export var hover_speed: float = 2.0

var base_y_position: float
var hover_timer: float = 0.0
var fly_texture: Texture2D

func _ready():
	# 设置Flying Creature属性
	max_health = 40
	speed = 80.0
	damage = 12
	detection_range = 120.0
	attack_range = 30.0
	
	# 加载纹理
	fly_texture = load("res://Art/v1.1 dungeon crawler 16X16 pixel pack/enemies/flying creature/fly_anim_spritesheet.png")
	
	base_y_position = global_position.y - fly_height
	
	super._ready()
	create_animations()

func create_animations():
	if not animation_player:
		return
	
	# 使用父类的安全函数获取或创建动画库
	var library = get_or_create_animation_library()
	
	# 创建飞行动画（只在不存在时创建）
	if not library.has_animation("fly"):
		var fly_animation = Animation.new()
		fly_animation.length = 0.4
		fly_animation.loop_mode = Animation.LOOP_LINEAR
		
		# 添加帧动画轨道
		var track_index = fly_animation.add_track(Animation.TYPE_VALUE)
		fly_animation.track_set_path(track_index, NodePath("Sprite2D:frame"))
		fly_animation.track_insert_key(track_index, 0.0, 0)
		fly_animation.track_insert_key(track_index, 0.1, 1)
		fly_animation.track_insert_key(track_index, 0.2, 2)
		fly_animation.track_insert_key(track_index, 0.3, 3)
		
		library.add_animation("fly", fly_animation)

func _physics_process(delta):
	hover_timer += delta
	update_hover_effect(delta)
	super._physics_process(delta)
	update_animation()

func update_hover_effect(delta):
	# 添加悬浮效果
	var hover_offset = sin(hover_timer * hover_speed) * hover_amplitude
	global_position.y = base_y_position + hover_offset

# 重写边界检测函数以适应飞行生物
func clamp_to_screen_bounds():
	# 如果游戏边界还没有正确设置，尝试重新设置
	if game_bounds.size.x <= 0 or game_bounds.size.y <= 0:
		setup_screen_bounds()
		return
	
	# 限制X轴位置
	var margin = 8.0
	var clamped_x = clamp(global_position.x, game_bounds.position.x + margin, 
						 game_bounds.position.x + game_bounds.size.x - margin)
	
	# 限制base_y_position而不是直接限制Y轴位置，以保持悬浮效果
	var min_y = game_bounds.position.y + margin + hover_amplitude
	var max_y = game_bounds.position.y + game_bounds.size.y - margin - hover_amplitude
	base_y_position = clamp(base_y_position, min_y, max_y)
	
	# 更新实际位置
	global_position.x = clamped_x
	var hover_offset = sin(hover_timer * hover_speed) * hover_amplitude
	global_position.y = base_y_position + hover_offset

func handle_chase_state(delta):
	if player:
		var direction = (player.global_position - global_position).normalized()
		# 飞行生物可以穿越地形
		velocity = direction * speed
		
		# 保持飞行高度
		base_y_position = player.global_position.y - fly_height
		
		var distance_to_player = global_position.distance_to(player.global_position)
		if distance_to_player <= attack_range and attack_timer <= 0:
			change_state(State.ATTACK)

func handle_idle_state(delta):
	velocity = Vector2.ZERO
	# 随机飞行
	if state_timer >= 3.0:
		var random_direction = Vector2(randf_range(-1, 1), randf_range(-1, 1)).normalized()
		velocity = random_direction * (speed * 0.3)
		state_timer = 0.0

func update_animation():
	if is_dead:
		return
		
	sprite.texture = fly_texture
	if animation_player.current_animation != "fly":
		safe_play_animation("fly")
